package com.gr.controller;

import com.gr.entity.User;
import com.gr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(User user, HttpSession session, HttpServletRequest req){
        String safeCode = req.getParameter("checkCode");
        String str=(String) req.getSession().getAttribute("checkCode");
        if (safeCode.equals(str)){
            User user1=userService.login(user);
            if (user1!=null){
                session.setAttribute("user",user1);
                return "/book/selectAllBook.do";
            }
        }
            return "/login.jsp";//前面不用加user
    }
    @RequestMapping("/register")
    public String register(User user){
        userService.register(user);
        return "/login.jsp";
    }
}
